﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using MBSPL;
using MBSBLC;
using MBSBusinessEntities;
using MBSProcessComponent;
using MBSVO;
namespace MBS
{
    public partial class SeatLayout : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CreateDynamicTable();
            }
        }
        public void CreateDynamicTable()
        {
            BLC blc = new BLC();
            MovieBookingSession mbs = (MovieBookingSession)Session["MBS"];
            ShowTimeVO showTimeVO = blc.GetShowTimeByID(mbs.ShowTimeId);
            string xml = showTimeVO.SeatsAvailable;

            SeatLayoutPlaceHolder.Controls.Clear();
            Dictionary<string, string> dictionary = blc.getShowTimeSeatDictionary(mbs.ShowTimeId);
            // Fetch the number of Rows and Columns for the table 
            // using the properties
            int tblRows = 5;
            int tblCols = 8;
            // Create a Table and set its properties 
            Table tbl = new Table();
            // Add the table to the placeholder control
            SeatLayoutPlaceHolder.Controls.Add(tbl);
            // Now iterate through the table and add your controls
            // Store keys in a List
            List<string> list = dictionary.Keys.ToList();
            int count = 0;
            for (int i = 0; i < tblRows; i++)
            {
                TableRow tr = new TableRow();
                for (int j = 0; j < tblCols; j++)
                {
                    TableCell tc = new TableCell();
                    Button btn = new Button();
                    string key = list[count];
                    String status = dictionary[key];
                    btn.Text = key;
                    btn.ID = key;
               
                    if (status == "Available")
                    {
                        btn.CssClass = "seat";
                    }
                    else if (status == "Booked")
                    {
                        btn.CssClass = "seatSold";
                        btn.Enabled = false;
                    }
                    else if (status == "Blocked")
                    {
                        btn.CssClass = "seatBlocked";
                        btn.Enabled = false;
                    }
                    tc.Controls.Add(btn);
                    tr.Cells.Add(tc);
                    count++;
                }
                tbl.Rows.Add(tr);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BLC blc = new BLC();
            if (Session["MBS"] != null)
            {
                MovieBookingSession  mbs = (MovieBookingSession)Session["MBS"];               
                String seatSelected = HiddenField1.Value;
                String[] seats = seatSelected.Split('|');
                foreach (String seat in seats)
                {
                    if (!String.IsNullOrEmpty(seat))
                    {
                        mbs.SelectedSeatsList.Add(seat);
                    }
                }
               bool isBlocked = blc.blockAvailableSeatsInCache(mbs.ShowTimeId, mbs.SelectedSeatsList);
               Response.Redirect("~/Booking.aspx");
            }
            else
            {   //may have to create this page..
                Response.Redirect("~/Error.aspx");
            }
        }
    }
}